package com.spzx.product.controller;

import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.product.api.domain.vo.CategoryVo;
import com.spzx.product.service.ICategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 商品分类Controller
 */
@Tag(name = "商品分类接口管理")
@RestController
@RequestMapping("/category")
public class CategoryController extends BaseController {

    @Autowired
    private ICategoryService categoryService;

    @Operation(summary = "获取分类下拉树列表")
    @GetMapping(value = "/treeSelect/{id}")
    public AjaxResult treeSelect(@PathVariable Long id) {
        return success(categoryService.treeSelect(id));
    }

/*    @Operation(summary = "获取分类下拉树列表")
    @GetMapping(value = "/treeSelect/{id}")
    public AjaxResult treeSelect(@PathVariable Long id) {
        long start = System.currentTimeMillis();
        List<Category> categoryList = null;
        for (int i = 0; i < 1000; i++) {
            categoryList = categoryService.treeSelect(id);
        }
        long total = System.currentTimeMillis() - start;
        System.out.println("total = " + total);
        return success(categoryList);
    }*/

    @Operation(summary = "Excel导出,文件下载")
    @PostMapping("/export")
    public void exportExcel(HttpServletResponse response){
        categoryService.exportExcel(response);
    }

    //<input class="el-upload__input" name="file" accept=".xlsx, .xls" type="file">
    @Operation(summary = "导入分类数据")
    @PostMapping("/import")
    public AjaxResult importData(MultipartFile file) throws Exception {
        try {
            categoryService.importData(file);
            return AjaxResult.success("导入成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error("导入失败");
    }


    //===给前台系统使用的服务接口=============================================

    /**
     * 首页 一级分类集合
     * @return
     */
    @InnerAuth
    @GetMapping(value = "/getOneCategory")
    public R<List<CategoryVo>> getOneCategory(){
        List<CategoryVo> oneCategoryList = categoryService.getOneCategory();
        return R.ok(oneCategoryList);
    }

    /**
     * 获取三级分类（立即加载）
     * @return
     */
    @InnerAuth
    @GetMapping(value = "/tree")
    public R<List<CategoryVo> > tree() {
        return R.ok(categoryService.tree());
    }

}